<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test exporting a private RSA key.");

jsTestIsAsync = true;

var extractable = true;

var privateKeyPkcs8Hex = "3081B6020100301006072A8648CE3D020106052B8104002204819E30819B0201010430A492CE8FA90084C227E1A32F7974D39E9FF67A7E8705EC3419B35FB607582BEBD461E0B1520AC76EC2DD4E9B63EBAE71A16403620004E55FEE6C49D8D523F5CE7BF9C0425CE4FF650708B7DE5CFB095901523979A7F042602DB30854735369813B5C3F5EF86828F59CC5DC509892A988D38A8E2519DE3D0C4FD0FBDB0993E38F18506C17606C5E24249246F1CE94983A5361C5BE983E";

debug("\nImporting a PKCS8 key...");
crypto.subtle.importKey("pkcs8", hexStringToUint8Array(privateKeyPkcs8Hex), {name: "ECDSA", namedCurve: "P-384" }, extractable, ['sign']).then(function(result) {
    key = result;

    debug("\nExporting the key as JWK...");
    return crypto.subtle.exportKey("jwk", key);
}).then(function(result) {
    exportedJWK = result;

    shouldEvaluateAs("exportedJWK.kty", "EC");
    shouldEvaluateAs("exportedJWK.crv", "P-384");
    shouldEvaluateAs("exportedJWK.d", "pJLOj6kAhMIn4aMveXTTnp_2en6HBew0GbNftgdYK-vUYeCxUgrHbsLdTptj665x");
    shouldEvaluateAs("exportedJWK.x", "5V_ubEnY1SP1znv5wEJc5P9lBwi33lz7CVkBUjl5p_BCYC2zCFRzU2mBO1w_Xvho");
    shouldEvaluateAs("exportedJWK.y", "KPWcxdxQmJKpiNOKjiUZ3j0MT9D72wmT448YUGwXYGxeJCSSRvHOlJg6U2HFvpg-");
    shouldBe("exportedJWK.alg", "undefined");
    shouldBe("exportedJWK.ext", "true");
    shouldBe("exportedJWK.key_ops", "['sign']");
    shouldBe("exportedJWK.use", "undefined");

    debug("\nExporting the key as PKCS8...");
    return crypto.subtle.exportKey("pkcs8", key);
}).then(function(result) {
    exportedPkcs8 = result;

    bytesShouldMatchHexString("exportedPkcs8", privateKeyPkcs8Hex, exportedPkcs8);
}).then(finishJSTest, failAndFinishJSTest);

</script>

</body>
</html>
